package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangfupeng
 */
public class MybatisPluginHtmlBackPage extends PluginAdapter {

    private FullyQualifiedJavaType slf4jLogger;
    private FullyQualifiedJavaType slf4jLoggerFactory;
    private FullyQualifiedJavaType autowired;
    private FullyQualifiedJavaType mediaType;
    private FullyQualifiedJavaType pathVariable;
    private FullyQualifiedJavaType requestBody;
    private FullyQualifiedJavaType requestHeader;
    private FullyQualifiedJavaType requestMapping;
    private FullyQualifiedJavaType requestMethod;
    private FullyQualifiedJavaType requestParam;
    private FullyQualifiedJavaType restController;
    private FullyQualifiedJavaType checkLimit;
    private FullyQualifiedJavaType authUser;
    private FullyQualifiedJavaType logging;
    private FullyQualifiedJavaType api;
    private FullyQualifiedJavaType apiOperation;
    private FullyQualifiedJavaType apiParam;

    private FullyQualifiedJavaType controllerType;

    private FullyQualifiedJavaType serviceType;
    private FullyQualifiedJavaType modelType;
    private FullyQualifiedJavaType modelExampleType;
    private FullyQualifiedJavaType javaUtilListType;


    private String basePackage;
    private String aspectPackage;
    private String servicePackage;
    private String controllerPackage;
    private String project;
    private String modelUrl;
    private String enableGenerateHtml;
    private String htmlPath;
    private String serverUrl;
    private String fileServer;
    private String encryptPasswordMethod;


    private FullyQualifiedJavaType stringType;
    private FullyQualifiedJavaType integerType;
    private FullyQualifiedJavaType booleanType;
    private FullyQualifiedJavaType byteType;
    private FullyQualifiedJavaType doubleType;
    private FullyQualifiedJavaType floatType;
    private FullyQualifiedJavaType longType;
    private FullyQualifiedJavaType shortType;
    private FullyQualifiedJavaType bigDecimalType;
    private FullyQualifiedJavaType dateType;

    /**
     * 所有的方法
     */
    private List<Method> methods;
    private String tokenErrorStatus;

    /**
     * 是否添加注解
     */

    public MybatisPluginHtmlBackPage() {
        super();
        methods = new ArrayList<Method>();
        // default is slf4j
        slf4jLogger = new FullyQualifiedJavaType("org.slf4j.Logger");
        slf4jLoggerFactory = new FullyQualifiedJavaType("org.slf4j.LoggerFactory");
        autowired = new FullyQualifiedJavaType("org.springframework.beans.factory.annotation.Autowired");
        mediaType = new FullyQualifiedJavaType("org.springframework.http.MediaType");
        pathVariable = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.PathVariable");
        requestBody = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestBody");
        requestMapping = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestMapping");
        requestMethod = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestMethod");
        requestParam = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RequestParam");
        restController = new FullyQualifiedJavaType("org.springframework.web.bind.annotation.RestController");
        api = new FullyQualifiedJavaType("io.swagger.annotations.Api");
        apiOperation = new FullyQualifiedJavaType("io.swagger.annotations.ApiOperation");
        apiParam = new FullyQualifiedJavaType("io.swagger.annotations.ApiParam");
        stringType = new FullyQualifiedJavaType("java.lang.String");
        integerType = new FullyQualifiedJavaType("java.lang.Integer");
        booleanType = new FullyQualifiedJavaType("java.lang.Boolean");
        byteType = new FullyQualifiedJavaType("java.lang.Byte");
        doubleType = new FullyQualifiedJavaType("java.lang.Double");
        floatType = new FullyQualifiedJavaType("java.lang.Float");
        longType = new FullyQualifiedJavaType("java.lang.Long");
        shortType = new FullyQualifiedJavaType("java.lang.Short");
        bigDecimalType = new FullyQualifiedJavaType("java.lang.BigDecimal");
        dateType = new FullyQualifiedJavaType("java.util.Date");

    }

    /**
     * 读取配置文件
     */
    @Override
    public boolean validate(List<String> warnings) {
        this.modelUrl = context.getJavaModelGeneratorConfiguration().getTargetPackage();
        this.basePackage = properties.getProperty("basePackage");
        this.fileServer = properties.getProperty("fileServer");
        this.servicePackage = properties.getProperty("servicePackage");
        this.project = properties.getProperty("targetProject");
        this.controllerPackage = properties.getProperty("controllerPackage");
        this.aspectPackage = properties.getProperty("aspectPackage");
        checkLimit = new FullyQualifiedJavaType(aspectPackage + ".CheckLimit");
        authUser = new FullyQualifiedJavaType(aspectPackage + ".AuthUser");
        logging = new FullyQualifiedJavaType(aspectPackage + ".Logging");
        this.enableGenerateHtml = properties.getProperty("enableGenerateHtml");
        this.htmlPath = properties.getProperty("htmlPath");
        this.encryptPasswordMethod = properties.getProperty("encryptPasswordMethod");
        this.serverUrl = properties.getProperty("serverUrl");
        this.tokenErrorStatus = properties.getProperty("tokenErrorStatus");
        return true;
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        List<GeneratedJavaFile> files = new ArrayList<GeneratedJavaFile>();
        addBackHtml(introspectedTable);
        return files;
    }

    private void addBackHtml(IntrospectedTable introspectedTable) {
        MybatisUtilHtmlBack.writeAllPage(introspectedTable, htmlPath, modelUrl, fileServer, serverUrl, basePackage, aspectPackage, tokenErrorStatus, encryptPasswordMethod);
        MybatisUtilHtmlImageBack.writeAllImage(introspectedTable, htmlPath, modelUrl);
    }

}
